name: Tests (Storage with server)

on:
  workflow_dispatch:
  push:
    branches:
      - master
  pull_request: {}
  schedule:
    - cron: '0 23 * * SUN-THU'

concurrency:
  group: ${{ github.workflow }}-${{ github.ref == 'refs/heads/master' && github.run_number || github.ref }}
  cancel-in-progress: true

jobs:
  # TODO(masap): Modify job name to "tests-storage-with-server" because this test is not only for
  # RDB. Since current name "tests-rdbstorage" is required in the Branch protection rules, you
  # need to modify the Branch protection rules as well.
  tests-rdbstorage:
    if: (github.event_name == 'schedule' && github.repository == 'optuna/optuna') || (github.event_name != 'schedule')
    runs-on: ubuntu-latest

    strategy:
      matrix:
        python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
        test-trigger-type:
          - ${{ (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') && 'Scheduled' || '' }}
        exclude:
          - test-trigger-type: ""
            python-version: "3.8"
          - test-trigger-type: ""
            python-version: "3.9"
          - test-trigger-type: ""
            python-version: "3.10"

    services:
      mysql:
        image: mysql:5.7
        ports:
          - 3306:3306
        env:
          MYSQL_ROOT_PASSWORD: mandatory_arguments
          MYSQL_DATABASE: optunatest
          MYSQL_USER: user
          MYSQL_PASSWORD: test
        options: >-
          --health-cmd "mysqladmin ping -h localhost"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      postgres:
        image: postgres:10.1-alpine
        ports:
          - 5432:5432
        env:
          POSTGRES_DB: optunatest
          POSTGRES_USER: user
          POSTGRES_PASSWORD: test
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      redis:
        image: redis
        ports:
          - 6379:6379

    steps:
    - name: Checkout
      uses: actions/checkout@v4

    - name: Setup Python${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}

    - name: Setup cache
      uses: actions/cache@v3
      env:
        cache-name: test-storage-with-server
      with:
        path: ~/.cache/pip
        key: ${{ runner.os }}-${{ matrix.python-version }}-${{ env.cache-name }}-${{ hashFiles('**/pyproject.toml') }}-v1
        restore-keys: |
          ${{ runner.os }}-${{ matrix.python-version }}-${{ env.cache-name }}-${{ hashFiles('**/pyproject.toml') }}

    - name: Setup environment
      run: |
        sudo apt-get update
        sudo apt-get -y install openmpi-bin libopenmpi-dev

    - name: Install
      run: |
        python -m pip install --upgrade pip
        pip install --progress-bar off -U setuptools

        # Install minimal dependencies and confirm that `import optuna` is successful.
        pip install --progress-bar off .
        python -c 'import optuna'
        optuna --version

        pip install --progress-bar off .[test]
        pip install --progress-bar off .[optional] --extra-index-url https://download.pytorch.org/whl/cpu

    - name: Install DB bindings
      run: |
        pip install --progress-bar off PyMySQL cryptography psycopg2-binary redis

    - name: Output installed packages
      run: |
        pip freeze --all
    - name: Output dependency tree
      run: |
        pip install pipdeptree
        pipdeptree

    - name: Tests MySQL
      run: |
        pytest tests/storages_tests/test_with_server.py
      env:
        SQLALCHEMY_WARN_20: 1
        OMP_NUM_THREADS: 1
        TEST_DB_URL: mysql+pymysql://user:test@127.0.0.1/optunatest

    - name: Tests PostgreSQL
      run: |
        pytest tests/storages_tests/test_with_server.py
      env:
        OMP_NUM_THREADS: 1
        TEST_DB_URL: postgresql+psycopg2://user:test@127.0.0.1/optunatest

    - name: Tests Journal Redis
      run: |
        pytest tests/storages_tests/test_with_server.py
      env:
        OMP_NUM_THREADS: 1
        TEST_DB_URL: redis://localhost:6379
        TEST_DB_MODE: journal-redis
